package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetMealMapper setMealMapper;
    @Override
    public PageResult queryDishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishDTO> page = dishMapper.queryDishPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public DishVO queryCategoryById(long id) {
        DishVO dish = dishMapper.queryDishById(id);
//       查询 dishFlavor
          List<DishFlavor> flavor =  dishFlavorMapper.queryDishFlavor(dish.getId());
          dish.setFlavors(flavor);
        return dish;
    }

    @Override
    @Transactional // 开启事务
    public void insetDish(DishDTO dishDTO) {
        // 向菜品表插入
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insetDish(dish);
        // 向口味表 多条插入
        List<DishFlavor> list =dishDTO.getFlavors();
        Long disId = dish.getId();
        if(list != null || !list.isEmpty()){
            list.forEach((e)->{
                e.setDishId(disId);
            });
            dishFlavorMapper.insertFave(list);
        }
    }

    @Override
    @Transactional
    public Result deleteDish(List<Long> ids) {
        log.info("菜品删除",ids);
        // 判断当前是否被套餐关联
        Integer len = setMealMapper.countByCategoryId(ids);
        if( len > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        ids.forEach(id ->{
          DishVO dish =  dishMapper.queryDishById(id);
            //   判断当前是否为 在售
            if(dish.getStatus() == StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // 删除菜品表数据
        dishMapper.deletDishByIds(ids);
        // 删除口味表中的数据
        dishFlavorMapper.deleteByDishId(ids);
        return Result.success();
    }

    @Override
    @Transactional
    public Result updateDish(DishVO vo) {
         Dish dish = new Dish();
         BeanUtils.copyProperties(vo,dish);
         // 更新
        dishMapper.upDateDish(dish);
        //  删除口味表中的数据 重新添加

        Long dish_id = vo.getId();
//        查询是否有
        List<DishFlavor> dishFlavors = dishFlavorMapper.queryDishFlavor(dish_id);
//        如果口味表中存在开始删除
        if(dishFlavors!= null && dishFlavors.size()>0){
            List<Long> longs = new ArrayList<>();
            longs.add(dish_id);
            dishFlavorMapper.deleteByDishId(longs);
            List<Long> ids = vo.getFlavors().stream().map(e->e.getId()).collect(Collectors.toList());
            if(ids!=null && ids.size()>0){
                dishFlavorMapper.deleteByDishId(ids);
            }
        }
        // 如果有口味 添加flavor
        List<DishFlavor> flavors = vo.getFlavors();
        if(flavors.size()>0 && flavors !=null){
            flavors.forEach(e ->{
                e.setDishId(dish_id);
            });
            dishFlavorMapper.insertFave(flavors);
        }
        //  更新菜品表
        return Result.success("更新成功");
    }

    @Override
    public void changeStatusById(Long id,Integer status) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.upDateDish(dish);
    }
}
